import Vue from "vue";
import store from "@/store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import VueRouter from "vue-router";
import Login from "@/views/system/login.vue";
import { getCurrentMenu, getIgnoreList } from "@/api/system/permission";
import { setIgnoreList } from "@/utils/plugins/request";
import { handleTree } from "@/utils/func/array_to_tree";
import { formatMenu } from "@/utils/plugins/routerDeepComponent";
import Layout from "@/layout";
import { SET_MENU } from "@/store/types/menu";

Vue.use(VueRouter);

// 解决路由重复点击问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
var routerFlag = true; // 是否是第一次进入路由收入，是第一次则开始请求菜单数据
NProgress.configure({
  easing: "‘ease’", // 动画方式
  speed: 600, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});
const routes = [
  {
    path: "/login",
    name: "Login",
    component: Login,
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach(async function(to, from, next) {
  NProgress.start();
  /**
   * 判断路由是否是登录路由
   * 是：进入路由
   * 否：判断token
   */
  if (to.path === "/login") {
    routerFlag = true;
    next();
    return;
  }
  if (routerFlag) {
  //   getIgnoreList().then(res=>{
  //     const ignoreList = res.data.map((item) => {
  //       if (item.indexOf("]") !== -1) {
  //         item = item.split("]")[1];
  //       }
  //       if(item.indexOf("**") !== -1){
  //         item = item.replace("**","\\**")
  //       }
  //       return item
  //     });
  //     setIgnoreList(ignoreList) // 将数据保存到ignoreList中
  //     return getCurrentMenu({ menuTypeId: 1 })
  //   }).then(res=>{
  //     const tree = handleTree(res.data)
  //     const { rmenu, rrouter } = formatMenu(tree)
      router.addRoutes([
        {
          path: '/',
          name: 'Layout',
          component: Layout,
          children: [
            {
              path: '/',
              name: 'dashboard',
              component: () => import(/* webpackChunkName: "dashboard" */ '../views/other.vue')
            },
            {
              id:"/dict",
              name:"字典管理",
              path:"/dict",
              component:()=> import(/* webpackChunkName: "dict" */ '@/views/system/dict/index.vue')
            }
        ]
            // {
            //   path: "*",
            //   name: "page404",
            //   component: Page404
            // }
        }
      ])
      store.dispatch("menu/" + SET_MENU, [{
        id:"/dict",
        name:"字典管理",
        path:"/dict"
      }]);
      routerFlag = false;
      next({ ...to, replace: true });
  //   })

  //   return
  //   // await store.dispatch('Menu/addRouter') // 保持同步添加路由
  }
  next();
});
router.afterEach(function() {
  NProgress.done();
});

export default router;
